/**
 * Copyright 2009 by Christian Scheid. This file is part of the switch eclipse plugin and is licensed under the
 * GPL version 3. Please refer to the URL http://www.gnu.org/licenses/gpl-3.0.html for details.
 */
package org.digitalsoul.switchplugin.prefs.wizards;

import org.digitalsoul.switchplugin.model.SwitchSet;
import org.digitalsoul.switchplugin.prefs.ActionListUI;
import org.digitalsoul.switchplugin.prefs.ItemsUI;
import org.digitalsoul.switchplugin.prefs.MatcherListUI;
import org.digitalsoul.switchplugin.prefs.util.ItemListConfigurator;
import org.digitalsoul.switchplugin.prefs.util.UITools;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class ItemsWizard extends Wizard implements ItemSelectionChangedListener {
    
    private final Page page;
    protected Label labe;
    protected Text textField;
    protected Composite mainPanel;
    protected Button checkbox;
    private SwitchSet set;
    private ItemListConfigurator configurator;
    private MatcherListUI matcherListUI;
    private ActionListUI actionListUI;

    public ItemsWizard(SwitchSet set) {
        configurator = new ItemListConfigurator(set.getItems(), this);
        this.set = set;
        page = new Page(getPageId());
        addPage(page);
    }

    @Override
    public String getWindowTitle() {
        return "Edit Switch Set";
    }

    @Override
    public boolean canFinish() {
        return true;
    }

    @Override
    public boolean needsPreviousAndNextButtons() {
        return false;
    }
    
    private String getPageId() {
        return "switch.items.wizard";
    }

    private final class Page extends WizardPage {

        protected Page(String pageId) {
            super(pageId);
        }

        @Override
        public String getDescription() {
            return "Edit Switch Set '" +  set.getName() + "'";
        }

        public void createControl(Composite parent) {
            mainPanel = new Composite(parent, SWT.NONE);
            GridLayout gridLayout = new GridLayout(1, false);
            gridLayout.marginTop = 12;
            gridLayout.marginLeft = 12;
            mainPanel.setLayout(gridLayout);
            setControl(mainPanel);
            
            Composite itemsPanel = UITools.createGridPanel(mainPanel, 2);
            itemsPanel.setLayoutData(new GridData(GridData.FILL_BOTH));
            new ItemsUI().build(itemsPanel, configurator);
            
            Composite matchersPanel = UITools.createGridPanel(mainPanel, 2);
            matchersPanel.setLayoutData(new GridData(GridData.FILL_BOTH));
            matcherListUI = new MatcherListUI(configurator);
            matcherListUI.build(matchersPanel);
            
            Composite actionsPanel = UITools.createGridPanel(mainPanel, 2);
            actionsPanel.setLayoutData(new GridData(GridData.FILL_BOTH));
            actionListUI = new ActionListUI(configurator);
            actionListUI.build(actionsPanel); 
        }
    }

    @Override
    public boolean performFinish() {
        set.replaceItems(configurator.getItems());
        return true;
    }
    
    public void itemSelectionChanged() {
        actionListUI.itemSelectionChanged();
        matcherListUI.itemSelectionChanged();
    }
}
